<script setup lang="ts">
import dayjs from 'dayjs';

defineOptions({
  // name 作为一种规范最好必须写上并且和路由的name保持一致
  name: 'Huamingce'
});
import uploadExcel from '@/components/uploadExcel/index.vue';
import '@vue-office/excel/lib/index.css';
import { computed, ref } from 'vue';
import { ExcelData } from '@/components/uploadExcel/types';
import { formatDate, l2ArrMap, l2m } from '@/utils/utils';
import { clone } from 'lodash';

const excelData = ref<ExcelData[]>();

import { PersonObject } from '@/views/utils/huamingce/types';

const tableData = computed(() => {
  if (excelData.value) {
    const tableData = [];
    const baseInfoMap = l2m(excelData.value[1].results, '员工UserID');
    const workInfoMap = l2m(excelData.value[3].results, '员工UserID');
    const personInfoMap = l2m(excelData.value[4].results, '员工UserID');
    const educationInfoMap = l2ArrMap(excelData.value[5].results, '员工UserID');
    const linkInfoMap = l2ArrMap(excelData.value[8].results, '员工UserID');
    Object.keys(baseInfoMap).forEach((id, index) => {
      const firstStudy = getFirstStudy(educationInfoMap[id]);
      const highStudy = getHighStudy(educationInfoMap[id]);
      const startDate = formatDate(baseInfoMap[id]['入职时间']);
      const endDate = formatDate(dayjs(startDate).add(3, 'year').subtract(1, 'day'));
      const obj: PersonObject = {
        序号: index + 1,
        工号: baseInfoMap[id]['工号'],
        姓名: baseInfoMap[id]['姓名'],
        性别: personInfoMap[id]['性别'],
        一级部门: baseInfoMap[id]['1级部门'],
        一级部门负责人: baseInfoMap[id]['直属主管'],
        二级部门: baseInfoMap[id]['2级部门'],
        二级部门负责人: baseInfoMap[id]['2级部门'] ? baseInfoMap[id]['直属主管'] : '',
        三级部门: baseInfoMap[id]['3级部门'],
        三级部门负责人: baseInfoMap[id]['3级部门'] ? baseInfoMap[id]['直属主管'] : '',
        职位: baseInfoMap[id]['职位'],
        手机号: baseInfoMap[id]['手机号'],
        邮箱: baseInfoMap[id]['邮箱'],
        状态: workInfoMap[id]['员工类型'] === '实习' ? '实习' : workInfoMap[id]['员工状态'],
        保险地: baseInfoMap[id]['参保地'],
        合同开始时间: startDate,
        合同到期时间: endDate,
        实际转正日期: formatDate(workInfoMap[id]['实际转正日期']),
        户口所在地: personInfoMap[id]['身份证地址'],
        身份证: personInfoMap[id]['证件号码'],
        出生日期: formatDate(personInfoMap[id]['出生日期']),
        年龄: personInfoMap[id]['年龄（系统计算）'],
        工龄: personInfoMap[id]['工龄（系统计算）'],
        在职状态: workInfoMap[id]['员工状态'] && '在职',
        入职时间: formatDate(baseInfoMap[id]['入职时间']),
        '司龄/月': baseInfoMap[id]['司龄（系统计算）'],
        第一学历: firstStudy['学历'],
        是否统招: '',
        毕业院校: firstStudy['毕业院校'],
        专业: firstStudy['所学专业'],
        第一学历毕业时间: formatDate(firstStudy['毕业时间']),
        最高学历: highStudy['学历'],
        最高学历是否为统招: '',
        最高学历毕业院校: highStudy['毕业院校'],
        最高学历专业: highStudy['所学专业'],
        最高学历毕业时间: formatDate(highStudy['毕业时间']),
        婚否: personInfoMap[id]['婚姻状况'],
        家属姓名: linkInfoMap[id][0]['紧急联系人姓名'],
        家属联系方式: linkInfoMap[id][0]['联系人电话'],
        联系地址: personInfoMap[id][0]['住址'],
        政治面貌: personInfoMap[id]['政治面貌'],
        专业技术职务: '',
        资格证书: '',
        离职时间: '',
        离职原因: '',
        招聘HR: '',
        备注: ''
      };
      tableData.push(obj);
    });
    return tableData;
  } else {
    return [];
  }
});
const studyList = ['其他', '小学', '初中', '高中', '中专', '大专', '本科', '硕士', '博士'];

function getFirstStudy(arr) {
  if (arr.length === 1) {
    return arr[0];
  } else if (arr.length) {
    const studyMap = l2m(arr, '学历');
    for (let i = 0, count = studyList.length; i < count; i++)
      if (studyMap[studyList[i]]) {
        return studyMap[studyList[i]];
      }
  }
}

function getHighStudy(arr) {
  if (arr.length === 1) {
    return arr[0];
  } else if (arr.length) {
    const studyMap = l2m(arr, '学历');
    const copyStudylist = clone(studyList);
    copyStudylist.reverse();
    for (let i = 0, count = studyList.length; i < count; i++)
      if (studyMap[studyList[i]]) {
        return studyMap[studyList[i]];
      }
  }
}

const columns: TableColumnList = [
  { label: '序号', prop: '序号', fixed: 'left', width: '60' },
  { label: '工号', prop: '工号', fixed: 'left', width: '160' },
  { label: '姓名', prop: '姓名', fixed: 'left', width: '160' },
  { label: '性别', prop: '性别', width: '160' },
  { label: '一级部门', prop: '一级部门', width: '160' },
  { label: '一级部门负责人', prop: '一级部门负责人', width: '160' },
  { label: '二级部门', prop: '二级部门', width: '160' },
  { label: '二级部门负责人', prop: '二级部门负责人', width: '160' },
  { label: '三级部门', prop: '三级部门', width: '160' },
  { label: '三级部门负责人', prop: '三级部门负责人', width: '160' },
  { label: '职位', prop: '职位', width: '160' },
  { label: '手机号', prop: '手机号', width: '160' },
  { label: '邮箱', prop: '邮箱', width: '160' },
  { label: '状态', prop: '状态', width: '160' },
  { label: '保险地', prop: '保险地', width: '160' },
  { label: '合同开始时间', prop: '合同开始时间', width: '160' },
  { label: '合同到期时间', prop: '合同到期时间', width: '160' },
  { label: '实际转正日期', prop: '实际转正日期', width: '160' },
  { label: '户口所在地', prop: '户口所在地', width: '160' },
  { label: '身份证', prop: '身份证', width: '160' },
  { label: '出生日期', prop: '出生日期', width: '160' },
  { label: '年龄', prop: '年龄', width: '160' },
  { label: '工龄', prop: '工龄', width: '160' },
  { label: '在职状态', prop: '在职状态', width: '160' },
  { label: '入职时间', prop: '入职时间', width: '160' },
  { label: '司龄/月', prop: '司龄/月', width: '160' },
  { label: '第一学历', prop: '第一学历', width: '160' },
  { label: '是否统招', prop: '是否统招', width: '160' },
  { label: '毕业院校', prop: '毕业院校', width: '160' },
  { label: '专业', prop: '专业', width: '160' },
  { label: '第一学历毕业时间', prop: '第一学历毕业时间', width: '160' },
  { label: '最高学历', prop: '最高学历', width: '160' },
  { label: '最高学历是否为统招', prop: '最高学历是否为统招', width: '160' },
  { label: '最高学历毕业院校', prop: '最高学历毕业院校', width: '160' },
  { label: '最高学历专业', prop: '最高学历专业', width: '160' },
  { label: '最高学历毕业时间', prop: '最高学历毕业时间', width: '160' },
  { label: '婚否', prop: '婚否', width: '160' },
  { label: '家属姓名', prop: '家属姓名', width: '160' },
  { label: '家属联系方式', prop: '家属联系方式', width: '160' },
  { label: '联系地址', prop: '联系地址', width: '160' },
  { label: '政治面貌', prop: '政治面貌', width: '160' },
  { label: '专业技术职务', prop: '专业技术职务', width: '160' },
  { label: '资格证书', prop: '资格证书', width: '160' },
  { label: '离职时间', prop: '离职时间', width: '160' },
  { label: '离职原因', prop: '离职原因', width: '160' },
  { label: '招聘HR', prop: '招聘HR', width: '160' },
  { label: '备注', prop: '备注', width: '160' }
];

// import { utils, writeFile } from 'xlsx';
//
// /**
//  * @Description: 导出excel
//  */
// const exportExcel = () => {
//   const res: string[][] = tableData.value.map(item => {
//     const arr = [];
//     columns.forEach(column => {
//       arr.push(item[column.prop]);
//     });
//     return arr;
//   });
//   const titleList: string[] = [];
//   columns.forEach(column => {
//     titleList.push(column.label);
//   });
//   res.unshift(titleList);
//   const workSheet = utils.aoa_to_sheet(res);
//   const workBook = utils.book_new();
//   utils.book_append_sheet(workBook, workSheet, '花名册');
//   writeFile(workBook, '花名册.xlsx');
// };
import exportExcel from '@/components/exportExcel/index';

function doExportExcel() {
  exportExcel('#exportTable', '花名册', 1, '花名册.xlsx');
}
</script>

<template>
  <div>
    <el-row>
      <el-col :span="6">
        <uploadExcel v-model="excelData" btn-text="钉钉文件上传" :header-row-index="[0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0]" />
      </el-col>
    </el-row>
    <el-button type="primary" style="width: 100%" @click="doExportExcel">生成花名册</el-button>
    <el-table style="width: 100%" :data="tableData" height="700" id="exportTable">
      <template v-for="item in columns" :key="item.prop">
        <el-table-column v-bind="item" />
      </template>
    </el-table>
  </div>
</template>
